<?php

namespace App\Traits;

use App\Services\Api\Api;

trait TechTrait
{
    public function getTechList($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'custId' => $data['company_id'],
            'name' => $data['tech_name'],
            'domainId' => $data['domain2'],
            'authStatus' => $data['status'],
            'pageNo' => $data['page_number'],
            'pageSize' => $data['page_limit'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);

        if ($result) {
            return ['dataList' => $result['techs'], 'pageCount' => $result['pageTotal']];
        }
        return false;
    }

    public function getTechInfo($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'techId' => $data['id'],
            'custId' => $data['company_id'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result && $result['succ'] == 1) {
            $arrDomain = array_filter(explode(',', $result['domain']));
            return [
                'tech_name' => $result['name'],
                'tech_domain_id' => $arrDomain,
                'tech_product_name' => $result['prodName'],
                'tech_used_range' => $result['applies'],
                'tech_content' => $result['content'],
                'patent_list' => $result['patents'],
            ];
        }
        return false;
    }

    public function addCompanyTechInfo($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'custId' => $data['company_id'],
            'techId' => -1,
            'name' => $data['tech_name'],
            'domain' => ',' . $data['tech_domain_id'] . ',',
            'prodName' => $data['tech_product_name'],
            'applies' => $data['tech_used_range'],
            'content' => $data['tech_content'],
            'patents' => $data['patent_info'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        return ($result && $result['succ'] == 1) ? true : false;
    }

    public function editCompanyTechInfo($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'custId' => $data['company_id'],
            'techId' => $data['id'],
            'name' => $data['tech_name'],
            'domain' => ',' . $data['tech_domain_id'] . ',',
            'prodName' => $data['tech_product_name'],
            'applies' => $data['tech_used_range'],
            'content' => $data['tech_content'],
            'patents' => $data['patent_info'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        return ($result && $result['succ'] == 1) ? true : false;
    }

    public function checkCompanyTechInfo($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'techId' => $data['id'],
            'custId' => $data['company_id'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result && $result['succ'] == 1) {
            return true;
        }
        return false;
    }

    public function delCompanyTechInfo($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'techId' => $data['id'],
            'custId' => $data['company_id'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result && $result['succ'] == 1) {
            return true;
        }
        return false;
    }
}
